package com.example.mongo.controller;

import com.example.mongo.Model.bean.Menu;
import com.example.mongo.Model.dto.ResponseData;
import com.example.mongo.service.ChapterService;
import com.example.mongo.service.MenuService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    MenuService menuService;
    @Autowired
    ResponseData responseData;

    @RequestMapping(value = "/getMenuByComicId/{comicId}",method = RequestMethod.GET)
    public String getMenuByComicId(@PathVariable("comicId")int comicId){
        //responseData
        Menu menu = menuService.getMenuByComicId(comicId);
        if (null==menu){
            responseData.error("400","无此漫画","");
        }else {
            responseData.success("ok", JSONObject.fromObject(menu).toString());
        }
        return responseData.toString();
    }

    @RequestMapping(value = "/getMenusByComicType/{comicType}",method = RequestMethod.GET)
    public String getMenusByComicType(@PathVariable("comicType")String comicType){
        List<Menu> list = menuService.getMenusByComicType(comicType);
        if (null==list){
            responseData.error("400","无结果","");
        }else {
            responseData.success("ok", JSONArray.fromObject(list).toString());
        }
        return responseData.toString();

    }

    @RequestMapping(value = "/getMenusBySearch/{key}",method = RequestMethod.GET)
    public String getMenusBySearch(@PathVariable("key")String key){

        List<Menu> list = menuService.getMenusBySearch(key);
        if (null==list){
            responseData.error("400","无结果","");
        }else {
            responseData.success("ok", JSONArray.fromObject(list).toString());
        }
        return responseData.toString();


    }

    @RequestMapping(value = "/getMenusByPage/{pageNum}",method = RequestMethod.GET)
    public String getMenusByPage(@PathVariable("pageNum")int pageNum){
      //  return menuService.getPage(pageNum).toString();
        List<Menu> list = menuService.getPage(pageNum);
        if (null==list){
            responseData.error("400","无结果","");
        }else {
            responseData.success("ok", JSONArray.fromObject(list).toString());
        }
        return responseData.toString();

    }

}
